package com.tjetc.controller;

import com.tjetc.entity.User;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/9/5
 */
@Controller
@RequestMapping("/convert/")
public class _6ConvertController {

    /*
        解析异常 参数校验异常
        AbstractHandlerExceptionResolver.logException Resolved [org.springframework.web.bind.MethodArgumentNotValidException:
        Validation failed for argument [0] in public java.lang.String com.tjetc.controller._6ConvertController.f(com.tjetc.entity.User):
        [Field error in object 'user' on field 'date':
            rejected value [2025.9.5];
            类型不匹配
            codes [typeMismatch.user.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch];
            arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
            codes [user.date,date]; arguments []; default message [date]];
            default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date';
         Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date]
         for value [2025.9.5]]] ]

     */

    /*
        错误信息：
        org.springframework.validation.BeanPropertyBindingResult: 1 errors

        以下是Map中的内容：
        key：user
        value：User{username='zhangsan', age=15, date=Fri Sep 05 00:00:00 CST 2025, price=44444.232323, a=null}

        key: org.springframework.validation.BindingResult.user
        org.springframework.validation.BeanPropertyBindingResult: 1 errors
        Field error in object 'user' on field 'age': rejected value [15]; codes [Range.user.age,Range.age,Range.java.lang.Integer,Range]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.age,age]; arguments []; default message [age],10,1]; default message [需要在1和10之间]


     */


    /*
        @Validated: 用来校验前端传递过来的参数
        BindingResult： 如果有错误则会将信息封装到此对象中
        map： 既保存了数据信息，也保存了错误信息


     */

    @RequestMapping("auto")
    public String f(@Validated User user, BindingResult result, Map<String, Object> map){
        System.out.println(result);
        System.out.println(user);

        // 判断只要你发生了错误 就返回到对应页面
        if (result.hasErrors()) {
            return "";
        }

        map.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });

        return "ok";
    }



}